#include "classattribute.h"
#include "uml/class/umlclassitem.h"
#include "uml/class/classmethod.h"

ClassAttribute::ClassAttribute(QGraphicsItem *parent) : UmlTextChildItem(parent)
{
    this->_itemTypeFlags |= UmlClassAttributeType;

    this->setFont(FontPainter::font());

    QTextOption option = this->document()->defaultTextOption();
    option.setWrapMode(QTextOption::NoWrap);
    this->document()->setDefaultTextOption(option);

    // init uml settings
    this->_name = "attribute";
    this->_visibility = PUBLIC;
    this->_type = "void";

    // init related getter und setter methods
    this->_getter = 0;
    this->_setter = 0;

    // init Pens
    this->_publicPen.setColor(PUBLIC_COLOR);
    this->_privatePen.setColor(PRIVATE_COLOR);
    this->_protectedPen.setColor(PROTECTED_COLOR);

    this->_isHovered = false;

    this->updateUmlText();
}

ClassAttribute::~ClassAttribute()
{
    qDebug() << "deleting attribute:" << this->name();
}

UmlVisibility ClassAttribute::visibility()
{
    return this->_visibility;
}

void ClassAttribute::setVisibility(QString visibility)
{
    if (visibility.toLower() == "public") {
        this->_visibility = PUBLIC;
    }

    if (visibility.toLower() == "private") {
        this->_visibility = PRIVATE;
    }

    if (visibility.toLower() == "protected") {
        this->_visibility = PROTECTED;
    }

    this->updateUmlText();;
}

QString ClassAttribute::visibilityToString()
{
    switch(this->_visibility) {
        default : return "public"; break;
        case PRIVATE : return "private"; break;
        case PROTECTED : return "protected"; break;
    }
}

QString ClassAttribute::umlVisibility()
{
    switch(this->_visibility) {
        default : return "+"; break;
        case PRIVATE : return "-"; break;
        case PROTECTED : return "#"; break;
    }
}

QString ClassAttribute::returnType()
{
    return this->_type;
}

void ClassAttribute::setReturnType(QString type)
{
    this->_type = type.length() == 0 ? "void" : type;

    this->updateUmlText();
}

QRectF ClassAttribute::umlTextRect()
{
    return this->_umlTextRect;
}

void ClassAttribute::setUmlTextRect(QRectF textRect)
{
    this->_umlTextRect = textRect;
}

QRectF ClassAttribute::visibilityRect()
{
    return this->_visibilityRect;
}

void ClassAttribute::setVisiblityRect(QRectF visibilityRect)
{
    this->_visibilityRect = visibilityRect;
}

QPen ClassAttribute::visibilityPen()
{
    if (this->_visibility == PUBLIC) {
        return this->_publicPen;
    }
    else if (this->_visibility == PRIVATE) {
        return this->_privatePen;
    }
    else {
        return this->_protectedPen;
    }
}

ClassMethod* ClassAttribute::getGetter()
{
    return this->_getter;
}

void ClassAttribute::setGetter(ClassMethod *getter)
{   
    this->_getter = getter;
}

bool ClassAttribute::hasGetter()
{
    return this->_getter != 0;
}

ClassMethod* ClassAttribute::getSetter()
{
    return this->_setter;
}

void ClassAttribute::setSetter(ClassMethod *setter)
{
    this->_setter = setter;
}

bool ClassAttribute::hasSetter()
{
    return this->_setter != 0;
}

void ClassAttribute::setProject(UmlProject *project)
{
    UmlItem::setProject(project);

    // highlighter
    this->_highlighter = new UmlMethodHighlighter(this->document(), project->dataTypes());
}

UmlRelationalItem* ClassAttribute::boundingItem()
{
    return this->_classItem;
}

void ClassAttribute::setClassItem(UmlClassItem *classItem)
{
    this->_classItem = classItem;
}

void ClassAttribute::openEditor()
{
    this->_classItem->openAttributeEditor(this);
}

void ClassAttribute::updateUmlText()
{
    this->_umlText = QString("%1 : %2").arg(this->_type).arg(this->_name);
    this->_umlTextRect = FontPainter::textRect(this->_umlText);
    this->setPlainText(this->_umlText);
}
